package ae.pcfc.etks.online.web.controller.user.profile.update;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ae.pcfc.etks.online.common.delegate.ClientRegistrationDelegate;
import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.common.Constants;
import ae.pcfc.etks.online.web.controller.BaseController;
import ae.pcfc.etks.online.web.model.ClientLogin;
import ae.pcfc.etks.online.web.util.SecurityUtils;

/**
 * Controller that handles all {@link RegisterController}-related requests, i.e.: all
 * requests for /client/register/*
 * 
 * @author Mohtisham.Salahria
 */
@Controller
@RequestMapping(ViewPath.UPDATE_PROFILE_REDIRECT)
public class UpdateProfileRedirectController extends BaseController{

	private ClientRegistrationDelegate clientRegistrationDelegate;
	
	
	@Resource(name="clientRegistrationDelegate")
	public void setClientRegistrationDelegate(
			ClientRegistrationDelegate clientRegistrationDelegate) {
		this.clientRegistrationDelegate = clientRegistrationDelegate;
	}

	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /client/register/
	 * @see WEB-INF/jsp/client/register/form.jsp
	 */
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public String  form(HttpServletRequest request, @ModelAttribute(value = ConstantsAdmin.CLIENT_REGISTER_MODEL_KEY) ClientLogin command) 
	{
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String strUserName = null;
        
		if (principal instanceof UserDetails) 
		{       	
			strUserName = ((UserDetails)principal).getUsername();
        } 
		else 
		{
        	strUserName = principal.toString();
        }
		
		String trakheesId = null;
		if(strUserName != null)
		trakheesId = this.clientRegistrationDelegate.findTrakheesIdByUserName(strUserName);
		String[] clientId = trakheesId.split("-");
		
		if(clientId[0].endsWith(Constants.PERSON))
		{
		
			HttpSession session = request.getSession(true);
			session.setAttribute("trakheesId", trakheesId);
			
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("clientId", SecurityUtils.encrypt(trakheesId));
				
			return ViewUtils.getRedirect(ViewPath.USER_PERSON,PageType.EDIT, paramMap);
		}
		if(clientId[0].endsWith(Constants.COMPANY))
		{
		
			Map<String,String> paramMap = new HashMap<String,String>();
			paramMap.put("clientId", SecurityUtils.encrypt(trakheesId));
		
			return ViewUtils.getRedirect(ViewPath.USER_COMPANY,PageType.EDIT, paramMap);
		}
		return ViewUtils.DEFAULT_VIEW;
	}	
}

